package com.jerkin.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.data.Stat;

import java.util.Objects;

/**
 * @version V1.0
 * @ClassName: ZKNodeListener
 * @Description:
 * @author: huhongzhi
 * @date: 2019/12/16 16:47
 */
public class ZKNodeListener {

    //创建线程
    public static Thread listen(){
        return new Thread(()->{
            CuratorFramework curatorFramework = ZKFactory.createZK();
            for(;;){
                try {
                    Stat stat = curatorFramework.checkExists().forPath("/node2/node2");
                    if(Objects.nonNull(stat)){
                        System.out.println("检测到节点存在！！");
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("检测到节点生成!!!");
        });
    }
}
